package com.chuangjiangx.member.basic.web.controller;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrHasCoupon;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrHasCouponRepository;
import com.chuangjiangx.member.basic.ddd.domain.model.Member;
import com.chuangjiangx.member.basic.ddd.domain.model.MemberRepository;
import com.chuangjiangx.member.coupon.ddd.domain.model.SwicthEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/h5/sync/wechat")
@Api(value = "同步微信卡包状态", tags = "同步微信卡包状态")
public class SyncWechatStatusController {

    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private MbrHasCouponRepository mbrHasCouponRepository;


    @ApiOperation(value = "同步微信卡包", httpMethod = "GET")
    @RequestMapping("/coupon")
    public Response coupon(String code, Integer type) {
        switch (type) {
            //1 标识为会员卡
            case 1:
                Member member = memberRepository.fromMemberCardNum(code);
                if (member == null) {
                    throw new BaseException("9999", "找不到对应的会员");
                }
                member.syncWxStatus(SwicthEnum.ON);
                memberRepository.update(member);
                break;
            // 0 标识为卡券
            case 0:
                MbrHasCoupon mbrHasCoupon = mbrHasCouponRepository.fromVerifyCode(code);
                if (mbrHasCoupon == null) {
                    throw new BaseException("9999", "找不到领取卡券的记录");
                }
                mbrHasCoupon.wxSynced();
                mbrHasCouponRepository.update(mbrHasCoupon);
                break;
            default:
                throw new BaseException("9999", "未找到标识对应的卡券类型");
        }
        return ResponseUtils.successCamel();
    }

}
